#include "recommend.h"
#include "ui_recommend.h"

#include <QString>
#include <QtNetwork>
#include <QJsonObject>
#include <QJsonDocument>

extern QString token;
recommend::recommend(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::recommend)
{
    ui->setupUi(this);
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
}
recommend::~recommend()
{
    delete ui;
}

void recommend::GetDishList(QString token)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/dish/recommend"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    reply = accessManager->get(request);//发送GET请求
}

void recommend::SearchDishList(QString name, int index, int size)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/dish/search"));//设置url
     QJsonObject json,json1;
     json1.insert("name",name);
     json.insert("filter",json1);
     json.insert("index",index);
     json.insert("size",size);
     QJsonDocument jsonDocument;
     jsonDocument.setObject(json);
     postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}

void recommend::on_pushButton_clicked()
{
    //request.setUrl(QUrl("http://81.70.102.35:8082/user/dish/recommend"));//设置url
    //request.setRawHeader("Authorization",token.toLatin1());
    //reply = accessManager->get(request);//发送GET请求
    qDebug()<<"完成菜谱推荐的解析辣";
}
void recommend::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
        {
            QByteArray bytes = reply->readAll();
            //qDebug() << "许凡楠大帅哥";
            //qDebug() << bytes;
            QJsonParseError jsonError;
            QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
            QJsonObject object = doucment.object();
            qDebug()<<object;
            if (object.contains("code"))
            {
                QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
            }
            QString name;
            if(reply->url().toString()=="http://81.70.102.35:8082/user/dish/recommend"){
                this->rDishList.clear();
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isArray())
                {
                    QJsonArray array = value.toArray();
                    int nSize = array.size();
                    for(int i=0;i<nSize;i++)
                    {
                        QJsonValue value = array.at(i);
                        QJsonObject qobject = value.toObject();
                        if(qobject.contains("name"))
                        {
                            QJsonValue value = qobject.value("name");
                            name=value.toString();
                        }
                        if(qobject.contains("dishes"))
                        {
                             QJsonValue value = qobject.value("dishes");
                             QJsonArray array1 = value.toArray();
                             int nSize1 = array1.size();
                             for(int j=0;j<nSize1;j++)
                             {
                                 rDishInfo dishinfo;
                                 QJsonValue value = array1.at(j);
                                 QJsonObject qobject = value.toObject();
                                 if(qobject.contains("id"))
                                 {
                                      QJsonValue value = qobject.value("id");
                                      dishinfo.id=value.toInt();
                                 }
                                 if(qobject.contains("name"))
                                 {
                                      QJsonValue value = qobject.value("name");
                                      dishinfo.name=value.toString();
                                 }
                                 if(qobject.contains("material"))
                                 {
                                      QJsonValue value = qobject.value("material");
                                      dishinfo.material=value.toString();
                                 }
                                 if(qobject.contains("detail"))
                                 {
                                      QJsonValue value = qobject.value("detail");
                                      dishinfo.detail=value.toString();
                                 }
                                 if(qobject.contains("pic"))
                                 {
                                      QJsonValue value = qobject.value("pic");
                                      dishinfo.pic=value.toString();
                                 }
                                 if(qobject.contains("type"))
                                 {
                                      QJsonValue value = qobject.value("type");
                                      dishinfo.pic=value.toString();
                                 }
                                 if(qobject.contains("userId"))
                                 {
                                      QJsonValue value = qobject.value("userId");
                                      dishinfo.userId=value.toInt();
                                 }
                                 if(qobject.contains("likeCount"))
                                 {
                                      QJsonValue value = qobject.value("likeCount");
                                      dishinfo.likeCount=value.toInt();
                                 }
                                 if(qobject.contains("commentCount"))
                                 {
                                      QJsonValue value = qobject.value("commentCount");
                                      dishinfo.commentCount=value.toInt();
                                 }
                                 this->rDishList.push_back(dishinfo);
                             }
                        }
                    }
                }
            }
            }
            if(reply->url().toString()=="http://81.70.102.35:8082/user/dish/search"){
                this->rDishList.clear();
                if(object.contains("data"))
                {
                    QJsonValue value = object.value("data");
                    QJsonObject qobject = value.toObject();
                    //这里对于rows的处理与上面菜谱推荐对于dishes的处理是相同的
                    if(qobject.contains("rows"))
                    {
                        QJsonValue value = qobject.value("rows");
                        QJsonArray array1 = value.toArray();
                        int nSize1 = array1.size();
                        for(int j=0;j<nSize1;j++)
                        {
                            rDishInfo dishinfo;
                            QJsonValue value = array1.at(j);
                            QJsonObject qobject = value.toObject();
                            if(qobject.contains("id"))
                            {
                                 QJsonValue value = qobject.value("id");
                                 dishinfo.id=value.toInt();
                            }
                            if(qobject.contains("name"))
                            {
                                 QJsonValue value = qobject.value("name");
                                 dishinfo.name=value.toString();
                            }
                            if(qobject.contains("material"))
                            {
                                 QJsonValue value = qobject.value("material");
                                 dishinfo.material=value.toString();
                            }
                            if(qobject.contains("detail"))
                            {
                                 QJsonValue value = qobject.value("detail");
                                 dishinfo.detail=value.toString();
                            }
                            if(qobject.contains("pic"))
                            {
                                 QJsonValue value = qobject.value("pic");
                                 dishinfo.pic=value.toString();
                            }
                            if(qobject.contains("type"))
                            {
                                 QJsonValue value = qobject.value("type");
                                 dishinfo.pic=value.toString();
                            }
                            if(qobject.contains("userId"))
                            {
                                 QJsonValue value = qobject.value("userId");
                                 dishinfo.userId=value.toInt();
                            }
                            if(qobject.contains("likeCount"))
                            {
                                 QJsonValue value = qobject.value("likeCount");
                                 dishinfo.likeCount=value.toInt();
                            }
                            if(qobject.contains("commentCount"))
                            {
                                 QJsonValue value = qobject.value("commentCount");
                                 dishinfo.commentCount=value.toInt();
                            }
                            this->rDishList.push_back(dishinfo);
                        }
                    }
                }
            }
            qDebug()<<this->rDishList[0].detail;
            emit rgetDishListSuccess();

        }
        else
        {
            qDebug() << "finishedSlot errors here";
            qDebug( "found error .... code: %d\n", (int)reply->error());
            qDebug(qPrintable(reply->errorString()));
        }
        reply->deleteLater();
}


